package com.ht.modules.device.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.modules.device.entity.DateUtils;
import com.ht.modules.device.entity.DeviceInfo;
import com.ht.modules.device.entity.DeviceMonitorRecords;
import com.ht.modules.device.entity.DeviceMonitorRecordsVo;
import com.ht.modules.device.mapper.DeviceMonitorRecordsMapper;
import com.ht.modules.device.service.IDeviceInfoService;
import com.ht.modules.device.service.IDeviceMonitorRecordsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  仪表监测记录
 * @Author:
 * @Date: 2025-03-21
 * @Version: V1.0
 */
@Slf4j
@Service
public class DeviceMonitorRecordsServiceImpl extends ServiceImpl<DeviceMonitorRecordsMapper, DeviceMonitorRecords> implements IDeviceMonitorRecordsService {

    @Autowired
    private DeviceMonitorRecordsMapper deviceMonitorRecordsMapper;
    @Autowired
    private IDeviceInfoService deviceInfoService;

    /**
     * 分页列表查询
     *
     * @param deviceMonitorRecordsVo 参数
     * @param page                   分页对象
     * @param column                 默认排序列
     * @param order                  默认排序方式
     * @return 分页列表
     */
    @Override
    public Page<DeviceMonitorRecordsVo> list(Page<DeviceMonitorRecordsVo> page, DeviceMonitorRecordsVo deviceMonitorRecordsVo, String column, String order) {

        return page.setRecords(deviceMonitorRecordsMapper.list(page, deviceMonitorRecordsVo, column, order));
    }

    /**
     * 不分页列表查询
     *
     * @param deviceMonitorRecordsVo 参数
     * @param column                 默认排序列
     * @param order                  默认排序方式
     * @return 不分页列表
     */
    @Override
    public List<DeviceMonitorRecordsVo> listNoPage(DeviceMonitorRecordsVo deviceMonitorRecordsVo, String column, String order) {
        String partitionDate = deviceMonitorRecordsVo.getPartitionDate();
        if (Strings.isBlank(partitionDate)) {
            deviceMonitorRecordsVo.setPartitionDate(DateUtils.formatDate(new Date(), "yyyyMM"));
        }
        return deviceMonitorRecordsMapper.list(null, deviceMonitorRecordsVo, column, order);
    }

    /**
     * 添加
     *
     * @param deviceMonitorRecords 参数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean add(DeviceMonitorRecords deviceMonitorRecords) {
        Date dateTime = deviceMonitorRecords.getDateTime();
        if (null == dateTime) {
            deviceMonitorRecords.setDateTime(new Date());
        }
        Integer deviceId = deviceMonitorRecords.getDeviceId();
        DeviceInfo deviceInfo = deviceInfoService.queryById(deviceId);
        if (null == deviceInfo) {
            throw new RuntimeException("未找到对应设备！");
        }
        deviceMonitorRecords.setDeviceType(deviceInfo.getDeviceType());
        deviceMonitorRecords.setPartitionDate(DateUtils.formatDate(deviceMonitorRecords.getDateTime(), "yyyyMM"));
        return this.save(deviceMonitorRecords);
    }

    /**
     * 编辑
     *
     * @param deviceMonitorRecords 参数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(DeviceMonitorRecords deviceMonitorRecords) {
        System.out.println("deviceMonitorRecords = " + deviceMonitorRecords);
        DeviceMonitorRecords deviceMonitorRecordsEntity = this.getById(deviceMonitorRecords.getId());
        if (null == deviceMonitorRecordsEntity) {
            throw new RuntimeException("未找到对应实体！");
        }
        boolean b = this.updateById(deviceMonitorRecords);
        if (!b) {
            throw new RuntimeException("编辑失败！");
        }
    }

    /**
     * 通过id删除（逻辑删除）
     *
     * @param id 参数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) {
        DeviceMonitorRecords deviceMonitorRecords = this.getById(id);
        if (null == deviceMonitorRecords) {
            throw new RuntimeException("未找到对应实体！");
        }
        boolean b = this.removeById(id);
        if (!b) {
            throw new RuntimeException("删除失败！");
        }
    }

    /**
     * 批量删除（逻辑删除）
     *
     * @param ids 参数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatch(String ids) {
        if (Strings.isBlank(ids)) {
            throw new RuntimeException("参数不识别！");
        }
        boolean b = this.removeByIds(Arrays.asList(ids.split(",")));
        if (!b) {
            throw new RuntimeException("批量删除失败！");
        }
    }

    /**
     * 通过id查询
     *
     * @param id 参数
     * @return 查询结果
     */
    @Override
    public DeviceMonitorRecords queryById(Integer id) {
        DeviceMonitorRecords deviceMonitorRecords = this.getById(id);
        if (null == deviceMonitorRecords) {
            throw new RuntimeException("未找到对应实体！");
        }
        return deviceMonitorRecords;
    }

    /**
     * 通过id查询Vo
     *
     * @param id 参数
     * @return 查询结果
     */
    @Override
    public DeviceMonitorRecordsVo queryVoById(Integer id) {
        DeviceMonitorRecordsVo deviceMonitorRecordsVo = deviceMonitorRecordsMapper.getVoById(id);
        if (null == deviceMonitorRecordsVo) {
            throw new RuntimeException("未找到对应实体！");
        }
        return deviceMonitorRecordsVo;
    }

}
